package com.baba.emq.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author wulongbo
 * @version 1.0
 * @date 2020/6/8 15:14
 * @description
 */
public class JsonUtil {
    /**
     * 将如下格式的json格式转换为List<java对象>
     *     [
     *   {
     *     "label": "字段1",
     *     "key": "one",
     *     "width": 100,
     *     "checked": true
     *   },
     *   {
     *     "label": "字段2",
     *     "key": "two",
     *     "checked": true
     *   }
     * ]
     * @param jsonString
     * @param clazz
     * @param <T>
     * @return
     */
    public static  <T>  List<T> getListDTO(String jsonString, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        List<JSONObject> jsonUserSetColumsList = (List<JSONObject>) JSON.parse(jsonString);
        for(JSONObject jsonObject : jsonUserSetColumsList) {
            T obj = JSONObject.toJavaObject(jsonObject, clazz);
            list.add(obj);
        }
        return list;
    }


    /**
     * 获取JSONObject数组
     * @param jsonString
     * @return
     */
    public static List<JSONObject> getList(String jsonString) {
        return  (List<JSONObject>) JSON.parse(jsonString);
    }



    /**
     * String类型转换成JSONObject类型
     * @param string
     * @return
     */
    public  static JSONObject strToJson(String string){
        return JSONObject.parseObject(string);
    }


    /**
     * 递归读取所有的key
     *
     * @param jsonObject
     */
    public static StringBuffer getAllKey(JSONObject jsonObject) {
        StringBuffer stringBuffer = new StringBuffer();
        Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            stringBuffer.append(key.toString()).append("|");

            if (jsonObject.get(key) instanceof JSONObject) {
                JSONObject innerObject = (JSONObject) jsonObject.get(key);
                stringBuffer.append(getAllKey(innerObject));
            } else if (jsonObject.get(key) instanceof JSONArray) {
                JSONArray innerObject = (JSONArray) jsonObject.get(key);
                stringBuffer.append(getAllKey(innerObject));
            }
        }
        return stringBuffer;
    }

    public static StringBuffer getAllKey(JSONArray json1) {
        StringBuffer stringBuffer = new StringBuffer();
        if (json1 != null ) {
            Iterator i1 = json1.iterator();
            while (i1.hasNext()) {
                Object key = i1.next();
                if (key instanceof  JSONObject) {
                    JSONObject innerObject = (JSONObject) key;
                    stringBuffer.append(getAllKey(innerObject));
                } else if (key instanceof JSONArray) {
                    JSONArray innerObject = (JSONArray) key;
                    stringBuffer.append(getAllKey(innerObject));
                }else{
                }
            }
        }
        return stringBuffer;
    }


    /**
     * 单且只有一个key的时候调用
     * @param jsonObject
     * @return
     */
    public static String getFirstKey(JSONObject jsonObject) {
        Iterator<String> keys = jsonObject.keySet().iterator();// jsonObject.keys();
        while (keys.hasNext()) {
            return keys.next();
        }
        return null;
    }



    private final static String st1 = "{\"username\":\"tom\",\"age\":18,\"address\":[{\"province\":\"上海市\"},{\"city\":\"上海市\"},{\"disrtict\":\"静安区\"}]}";
    private final static String st2 = "{username:\"tom\",age:18}";
    private final static String st3="{username:\"tom\"}";
    private final static String st4="{\"register\": [{\"IMEI\":\"353456789012345\",\"hierarchy\":\" HQD\",\"manufacturer\": \"PL-151065\",\"subordinate\":\"木子李\",\"type\":\"监狱手环\"},{\"IMEI\":\"13131313131\",\"hierarchy\":\" HQD\",\"manufacturer\": \"PL-99999\",\"subordinate\":\"木子李\",\"type\":\"上来发个\"}]}";


    public static void main(String[] args) {
        System.out.println(st1);
        StringBuffer stb = getAllKey(strToJson(st1));
        System.err.println(stb);

        String firstValue= strToJson(st4).get(getFirstKey(strToJson(st4))).toString();
        getList(firstValue);

        String result=getFirstKey(strToJson(st3));
        System.out.println("result:" + result);


    }

}
